package com.hn658.base.message.notisender.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.hn658.base.message.common.utils.RedisKeyHelper;
import com.hn658.base.message.notisender.exception.NotiSenderException;
import com.hn658.base.message.notisender.message.NotificationMessage;
import com.hn658.framework.dataaccess.redis.AbstractRedisDAO;
@Component
public class DeviceTokenManagerImpl extends AbstractRedisDAO<NotificationMessage> implements IDeviceTokenManager {

	@Autowired
	@Qualifier("redisTemplate")
	@Override
	public void setTemplate(StringRedisTemplate template) {
		super.template = template;
	}
	
	@Override
	public void setApnsToken(String appKey, String userId, String apnsToken) {
		BoundHashOperations<String, String, String> userOps = getAccountInfo(appKey, userId);
		if(userOps==null){
			throw new NotiSenderException(NotiSenderException.NoUserInfoCache);
		}
		userOps.put("apnsToken", apnsToken);
	}

	@Override
	public void setChannelId(String appKey, String userId, Long channelId, String channelUserId) {
		BoundHashOperations<String, String, String> userOps = getAccountInfo(appKey, userId);
		if(userOps==null){
			throw new NotiSenderException(NotiSenderException.NoUserInfoCache);
		}
		userOps.put("channelId", channelId+"");
		userOps.put("userId", channelUserId);
	}

	@Override
	public void removeApnsToken(String appKey, String userId) {
		BoundHashOperations<String, String, String> userOps = getAccountInfo(appKey, userId);
		if(userOps!=null){
			userOps.delete("apnsToken");
		}
	}

	@Override
	public void removeChannelId(String appKey, String userId) {
		BoundHashOperations<String, String, String> userOps = getAccountInfo(appKey, userId);
		if(userOps!=null){
			userOps.delete("channelId");
			userOps.delete("userId");
		}
	}

	@Override
	public String getApnsToken(String appKey, String userId) {
		BoundHashOperations<String, String, String> userOps = getAccountInfo(appKey, userId);
		if(userOps==null){
			throw new NotiSenderException(NotiSenderException.NoUserInfoCache);
		}
		return userOps.get("apnsToken");
	}

	@Override
	public Long getChannelId(String appKey, String userId) {
		BoundHashOperations<String, String, String> userOps = getAccountInfo(appKey, userId);
		if(userOps==null){
			throw new NotiSenderException(NotiSenderException.NoUserInfoCache);
		}
		if(StringUtils.isNotBlank(userOps.get("channelId"))){
			return Long.parseLong(userOps.get("channelId"));
		}else{
			return null;
		}	
	}
	
	@Override
	public String getUserId(String appKey, String userId) {
		BoundHashOperations<String, String, String> userOps = getAccountInfo(appKey, userId);
		if(userOps==null){
			throw new NotiSenderException(NotiSenderException.NoUserInfoCache);
		}
		return userOps.get("userId");
	}

	/**
	 * 通过appKey和userId获取用户redis中的缓存hash
	 * @param appKey
	 * @param userId
	 * @return
	 */
	private BoundHashOperations<String, String, String> getAccountInfo(String appKey,String userId){
		if(StringUtils.isNotBlank(userId)&&StringUtils.isNotBlank(appKey)){
	        BoundHashOperations<String, String, String> userOps = template
	                .boundHashOps(RedisKeyHelper.accountInfo(appKey, userId));
	        return userOps;
		}else {
			return null;
		}
	}
}
